var REQKEY_COMMAND = "command";
var REQKEY_COMMAND_INIT = "init";
var REQKEY_COMMAND_REGIST = "regist";
var REQKEY_COMMAND_SEARCH = "search";
var REQKEY_COMMAND_EDIT = "edit";
var REQKEY_COMMAND_DELETE = "delete";
var REQKEY_COMMAND_POST = "post";
var REQKEY_PROC = "proc";
var RESKEY_ERROR = "error";
var RESKEY_MSG = "message";

var query = {};

var option_payoff = '<option value="2">馬連</option>'
	+ '<option value="3">馬単</option>'
	+ '<option value="4">３連複</option>'
	+ '<option value="5">３連単</option>';

var option_mark = '<option value="1">◎</option>'
	+ '<option value="2">&#9711;</option>'
	+ '<option value="3">▲</option>'
	+ '<option value="4">△</option>'
	+ '<option value="5">×</option>'
	+ '<option value="6">☆</option>';

var option_mark_blank = '<option value="">--</option>'	+ option_mark;


function params2json(d) {
    if (d.constructor != Array) {
        return d;
    }
    var data={};
    for(var i=0;i<d.length;i++) {
        if (typeof data[d[i].name] != 'undefined') {
            if (data[d[i].name].constructor!= Array) {
                data[d[i].name]=[data[d[i].name],d[i].value];
            } else {
                data[d[i].name].push(d[i].value);
            }
        } else {
            data[d[i].name]=d[i].value;
        }
    }
    return data;
};

//指定されたエリアの入力項目を取得する
var getInput = function(selector){
	var ret = {};
	$(selector).each(function(index){
		$(":input",$(this)).each(function(){
			name = $(this).attr("name");
			if(typeof name != 'undefined'){
				ret[name] = $(this).val();

				if($(this).attr("type") == "checkbox"){
					ret[name] = "";
				}
				
				if($(this).attr("type") == "radio"){
					ret[name] = "";
				}
			}
		});
		$("input:checked",$(this)).each(function(){
			name = $(this).attr("name");
			if(typeof name != 'undefined'){
				ret[name] = $(this).val();
			}
		});
	});
	return ret;
};

//指定された一覧エリアの入力項目を取得する
var getInputList = function(selector){
	var ret = [];
	$(selector).each(function(index){
		var map = {};
		$(":input",$(this)).each(function(){
			name = $(this).attr("name");
			if(typeof name != 'undefined'){
				map[name] = $(this).val();
				if($(this).attr("type") == "checkbox" && !$(this).attr('checked')){
					map[name] ="";
				}
			}
		});
		ret[index] = map;
	});
	return ret;
};

//レスポンスデータを設定する
var setResponseData = function(data,line,afterDo){
	var key = "";
	for( key in data){
		if(key == "0"){
			continue;
		}
		var selector = "#" + key;
		setValue(selector,data[key]);
		//$(selector).val(data[key]);
		//$(selector).text(data[key]);

		var str = null;
		if(line != null){
			str = line[key];
		}
		setAncestor(key,data[key],str)
	}
	if(afterDo){
		afterDo(data);
	}
}

function setAncestor(ance,data,str){
	if(data == null){
		return;
	}
	if(typeof data == "string"){
		return;
	}
	var key = "";
	for( key in data){
		if(str != null){
			$("." + ance + "Area").append(str);
		}
		if(isValidType(data[key])){
			setVal(ance,key,data);
			continue;
		}
		setDescendant(ance,data[key],str)
	}
}

function setDescendant(ance,data,str){
	if(data == null){
		return;
	}
	if(typeof data == "string"){
		return;
	}
	var key = "";
	for( key in data){
		if(isValidType(data[key])){
			setVal(ance,key,data);
			continue;
		}
	}
}

//値を設定する
function setVal(ance,key,data){
	var selector = "." + ance + " #" + key;
	setValue(selector,data[key]);
	selector = "." + ance + ":last *[name=" + key + "]";
	setValue(selector,data[key]);
}

//値を設定する
function setValue(selector,data){
	if($(selector).attr("type") == "checkbox"){
		$(selector).attr("checked",data[key]);
	}else if($(selector).attr("type") == "radio"){
		$(selector).val([data]);
	}else if($(selector).attr("type")){
		$(selector).val(data);
	}else if($(selector + ":parent").val()){
		$(selector).val(data);
	}else if($(selector).is("select")){
		$(selector).val(data);
	}else{
		$(selector).text(data);
	}
}

function isValidType(data){
	return typeof data == "string" || typeof data == "number" || typeof data == "boolean";
}

//金額合計
var total = function(selector){
	var total = 0;
	$(".wager").each(function(){
		var n = $(this).val();
		if(isNaN(n) || n === ""){
			n = 0;
		}
		total = total + parseInt(n);
	});
	total = total * 100; 
	$("#total").html(total);
	comma($("#total"));
}

//数値フォーマット
function formatNumber(ancestor){
	var selector = ".currency,.num";
	if(ancestor){
		selector = ancestor + " .currency," + ancestor + " .num"; 
	}
	comma($(selector));
	$(".percent").each(function(){ 
		$(this).append("%");
	});
}

//カンマ付与
function comma(selector){  
	return selector.each(function(){  
		var num3string = (setComma)(this.innerHTML);  
		this.innerHTML = num3string;  
	});
}

var setComma = function(num){  
	var str = new String(num);
	var n = "";  
	var count = 0;  
	for (var i=str.length-1; i>=0; i--){  
		n = str.charAt(i) + n;  
		count++;  
		if (((count % 3) == 0) && (i != 0)) n = ","+n;  
	}
	return n;  
}

//ajax
var b_ajax = function (url,query,doSuccess){
	$.ajax({
		type: "POST",
		url: url,
		data: query,
		success: function(json){
			if(json["error"]){
				$("#message").html(json[RESKEY_ERROR]);
			}else{
				if(doSuccess){
					doSuccess(json);
				}
			}
			setTimeout("clear()",5000);
		},
		error: function(request){
			$("#message").html("失敗");
		}
	});
}

//メッセージクリア
var clear = function(){
	$("#message").html("");
}
